//
//  CSHomeBottomView.m
//  ChinaShip
//
//  Created by Yale on 2019/1/25.
//  Copyright © 2019年 ChinaShip. All rights reserved.
//

#import "CSHomeBottomView.h"
@interface CSHomeBottomView (){
    CGSize itemSize;
    CGRect middleRect,leftRect,rightRect,bottomRect;
    CGRect transfromLeftRect,transfromRightRect,transfromBottomRect;
    NSInteger _fromIndex,_toIndex;
    BOOL isAnimateFinish;
}
@property (nonatomic,strong)UIImageView *middleImgView;
@property (nonatomic,strong)UIImageView *leftImgView;
@property (nonatomic,strong)UIImageView *rightImgView;
@property (nonatomic,strong)UIImageView *tempImgView;
@property (nonatomic,strong)NSArray     *imgList;
@end

@implementation CSHomeBottomView

+ (instancetype)createHomeBottomViewWithFrame:(CGRect)frame imgList:(NSArray *)imgList{
    CSHomeBottomView *homeBottomView = [[CSHomeBottomView alloc]initWithFrame:frame];
    homeBottomView->itemSize = CGSizeMake(80, 95);
    homeBottomView.imgList = imgList;
    [homeBottomView setUpByFrame:frame];
    return homeBottomView;
}

//设置
- (void)setUpByFrame:(CGRect)frame{
    middleRect = CGRectMake(self.center.x-itemSize.width/2, 58, itemSize.width, itemSize.height);
    leftRect = CGRectMake(20, 58+49, itemSize.width, itemSize.height);
    rightRect = CGRectMake(kSCREEN_WIDTH-itemSize.width-20, 58+49, itemSize.width, itemSize.height);
    bottomRect = CGRectMake(middleRect.origin.x, CGRectGetHeight(frame)+middleRect.size.height+58, middleRect.size.width, middleRect.size.height);
    
    self.middleImgView = [[UIImageView alloc] initWithFrame:middleRect];
    self.middleImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.middleImgView.layer.masksToBounds = YES;
    self.middleImgView.layer.cornerRadius = 5.0f;
    self.middleImgView.image = [UIImage imageNamed:self.imgList.firstObject];
    [self addSubview:self.middleImgView];
    
    self.leftImgView = [[UIImageView alloc]initWithFrame:leftRect];
    self.leftImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.leftImgView.layer.masksToBounds = YES;
    self.leftImgView.layer.cornerRadius = 5.0f;
    self.leftImgView.image = [UIImage imageNamed:self.imgList.lastObject];
    self.leftImgView.transform = CGAffineTransformMakeRotation(-M_PI/10);
    [self addSubview:self.leftImgView];
    
    self.rightImgView = [[UIImageView alloc]initWithFrame:rightRect];
    self.rightImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.rightImgView.layer.masksToBounds = YES;
    self.rightImgView.layer.cornerRadius = 5.0f;
    self.rightImgView.image = [UIImage imageNamed:self.imgList[1]];
    self.rightImgView.transform = CGAffineTransformMakeRotation(M_PI/10);
    [self addSubview:self.rightImgView];
    
    /*放在底部过渡*/
    self.tempImgView = [[UIImageView alloc]initWithFrame:bottomRect];
    self.tempImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.tempImgView.transform = CGAffineTransformMakeRotation(M_PI);
    self.tempImgView.layer.masksToBounds = YES;
    self.tempImgView.layer.cornerRadius = 5.0f;
    [self addSubview:self.tempImgView];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, frame.size.height-90, kSCREEN_WIDTH, 90)];
    bgImageView.image = [UIImage imageNamed:@"home_bottom_bg"];
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:bgImageView];
    
    transfromLeftRect = self.leftImgView.frame;
    transfromRightRect = self.rightImgView.frame;
    transfromBottomRect = self.tempImgView.frame;
    _fromIndex = 0;
    _toIndex = 0;
    isAnimateFinish = YES;
}

//4 - 0 - 1
//0 - 1 - 2
- (void)scrollowFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex{
    //初始状态
    if (fromIndex == toIndex) {
        return;
    }
    _fromIndex = fromIndex;
    _toIndex = toIndex;
    __weak typeof(self)weakSelf = self;
    if (fromIndex > toIndex)/*向左滑动*/ {
        if (fromIndex == self.imgList.count-1 && toIndex == 0) {
            [weakSelf moveRightScroll];
            return;
        }
        [weakSelf moveLeftScroll];
    }else/*向右滑动*/{
        if (fromIndex == 0 && toIndex == self.imgList.count-1) {
            [weakSelf moveLeftScroll];
            return;
        }
        [weakSelf moveRightScroll];
    }
}

//右滚动
- (void)moveRightScroll{
    __block typeof(leftRect)weakLeftRect = leftRect;
    __block typeof(rightRect)weakRightRect = rightRect;
    __block typeof(middleRect)weakMiddleRect = middleRect;
    __block typeof(bottomRect)weakBottomRect = bottomRect;
    __block typeof(itemSize)weakItemSize = itemSize;
    
    UIImageView *middleTempImgView = [self getMiddleImageView];
    UIImageView *leftTempImgView   = [self getLeftImageView];
    UIImageView *rightTempImgView  = [self getRightImageView];
    UIImageView *bottomTempImageView = [self getBottomImageView];
    
    if (!isAnimateFinish) {
        return;
    }
    isAnimateFinish = NO;
    
    [UIView animateWithDuration:0.1 animations:^{
         bottomTempImageView.frame = CGRectMake(kSCREEN_WIDTH-50, kSCREEN_HEIGHT-10, weakItemSize.width, weakItemSize.height);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        middleTempImgView.transform = CGAffineTransformIdentity;
        leftTempImgView.transform = CGAffineTransformIdentity;
        rightTempImgView.transform = CGAffineTransformIdentity;
        bottomTempImageView.transform = CGAffineTransformIdentity;
        
        middleTempImgView.frame = weakLeftRect;
        middleTempImgView.transform = CGAffineTransformMakeRotation(-M_PI/10);
        
        rightTempImgView.frame = weakMiddleRect;
    
        leftTempImgView.frame = CGRectMake(-100, kSCREEN_HEIGHT+20, weakItemSize.width, weakItemSize.height);
        
        bottomTempImageView.frame = weakRightRect;
        bottomTempImageView.transform = CGAffineTransformMakeRotation(M_PI/10);
        
    }completion:^(BOOL finished) {
        leftTempImgView.frame = weakBottomRect;
        if (CGRectEqualToRect(bottomTempImageView.frame, weakRightRect)) {
            bottomTempImageView.frame = weakRightRect;
        }
        isAnimateFinish = YES;
    }];
    
    if (_toIndex == self.imgList.count-1) {
        bottomTempImageView.image = [UIImage imageNamed:self.imgList.firstObject];
    }else{
        _toIndex++;
        bottomTempImageView.image = [UIImage imageNamed:[self.imgList objectAtIndex:_toIndex]];
    }
    
}

//左滚动
- (void)moveLeftScroll{
    
    __block typeof(leftRect)weakLeftRect = leftRect;
    __block typeof(rightRect)weakRightRect = rightRect;
    __block typeof(middleRect)weakMiddleRect = middleRect;
    __block typeof(bottomRect)weakBottomRect = bottomRect;
    __block typeof(itemSize)weakItemSize = itemSize;
    
    UIImageView *middleTempImgView = [self getMiddleImageView];
    UIImageView *leftTempImgView   = [self getLeftImageView];
    UIImageView *rightTempImgView  = [self getRightImageView];
    UIImageView *bottomTemImgView  = [self getBottomImageView];
    
    if (!isAnimateFinish) {
        return;
    }
    isAnimateFinish = NO;
    
    [UIView animateWithDuration:0.1 animations:^{
        bottomTemImgView.frame = CGRectMake(-100, kSCREEN_HEIGHT+20, weakItemSize.width, weakItemSize.height);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        middleTempImgView.transform = CGAffineTransformIdentity;
        leftTempImgView.transform = CGAffineTransformIdentity;
        rightTempImgView.transform = CGAffineTransformIdentity;
        bottomTemImgView.transform = CGAffineTransformIdentity;
        
        middleTempImgView.frame = weakRightRect;
        middleTempImgView.transform = CGAffineTransformMakeRotation(M_PI/10);
        leftTempImgView.frame = weakMiddleRect;
        
        rightTempImgView.frame = CGRectMake(kSCREEN_WIDTH+50, kSCREEN_HEIGHT+10, weakItemSize.width, weakItemSize.height);
        
        bottomTemImgView.frame = weakLeftRect;
        bottomTemImgView.transform = CGAffineTransformMakeRotation(-M_PI/10);
        
    } completion:^(BOOL finished) {
        rightTempImgView.frame = weakBottomRect;
        if (CGRectEqualToRect(bottomTemImgView.frame, weakLeftRect)) {
            bottomTemImgView.frame = weakLeftRect;
        }
        isAnimateFinish = YES;
    }];

    if (_toIndex == 0) {
        bottomTemImgView.image = [UIImage imageNamed:self.imgList.lastObject];
    }else{
        _toIndex--;
        bottomTemImgView.image = [UIImage imageNamed:[self.imgList objectAtIndex:_toIndex]];
    }
}



//获取中间视图
- (UIImageView *)getMiddleImageView{
    if (self.leftImgView.frame.origin.y == middleRect.origin.y) {
        return self.leftImgView;
    }
    if (self.rightImgView.frame.origin.y == middleRect.origin.y) {
        return self.rightImgView;
    }
    if (self.middleImgView.frame.origin.y == middleRect.origin.y) {
        return self.middleImgView;
    }
    if (self.tempImgView.frame.origin.y == middleRect.origin.y) {
        return self.tempImgView;
    }
    return nil;
}

//获取左边img
- (UIImageView *)getLeftImageView{
    if (self.leftImgView.frame.origin.x == transfromLeftRect.origin.x) {
        return self.leftImgView;
    }
    if (self.rightImgView.frame.origin.x == transfromLeftRect.origin.x) {
        return self.rightImgView;
    }
    if (self.middleImgView.frame.origin.x == transfromLeftRect.origin.x) {
        return self.middleImgView;
    }
    if (self.tempImgView.frame.origin.x == transfromLeftRect.origin.x) {
        return self.tempImgView;
    }
    return nil;
}

//获取右边img
- (UIImageView *)getRightImageView{
    if (self.leftImgView.frame.origin.x == transfromRightRect.origin.x) {
        return self.leftImgView;
    }
    if (self.rightImgView.frame.origin.x == transfromRightRect.origin.x) {
        return self.rightImgView;
    }
    if (self.middleImgView.frame.origin.x == transfromRightRect.origin.x) {
        return self.middleImgView;
    }
    if (self.tempImgView.frame.origin.x == transfromRightRect.origin.x) {
        return self.tempImgView;
    }
    return nil;
}

- (UIImageView *)getBottomImageView{
    if (self.leftImgView.frame.origin.y == transfromBottomRect.origin.y) {
        return self.leftImgView;
    }
    if (self.rightImgView.frame.origin.y == transfromBottomRect.origin.y) {
        return self.rightImgView;
    }
    if (self.middleImgView.frame.origin.y == transfromBottomRect.origin.y) {
        return self.middleImgView;
    }
    if (self.tempImgView.frame.origin.y == transfromBottomRect.origin.y) {
        return self.tempImgView;
    }
    return nil;
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
